//
//  FindNearMainViewController.m
//  HJZWeiBo
//
//  Created by lanou on 16/5/30.
//  Copyright © 2016年 蒋晨成. All rights reserved.
//

#import "FindNearMainViewController.h"
#import "RequestManager.h"
#import "FindMainTableViewCell.h"
#import "FindMainTableViewModel.h"
#import "NearListTableViewCell.h"
#import "WBHttpRequest.h"



@interface FindNearMainViewController ()<UITableViewDelegate, UITableViewDataSource, WBHttpRequestDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *listArray;
@property (nonatomic, strong) NSMutableArray *listArrayTwo;



@end

@implementation FindNearMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight + 44) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.listArray = [NSMutableArray array];
    self.listArrayTwo = [NSMutableArray array];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"新鲜事";
    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    self.navigationItem.leftBarButtonItem = left;
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"gengduo"] style:UIBarButtonItemStylePlain target:self action:@selector(moreAction:)];
    self.navigationItem.rightBarButtonItem = right;
    /*
//    [RequestManager requestWithUrl:kNearMainUrl requestType:RequestTypeGET parDic:nil finish:^(NSData *data) {
//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        NSArray *cardsArray = dic[@"cards"];
//        NSDictionary *cardsDic = cardsArray[0];
//        NSArray *card_group = cardsDic[@"card_group"];
//        NSDictionary *card_groupDic = card_group[0];
//        FindMainTableViewModel *model = [[FindMainTableViewModel alloc]init];
//        [model setValuesForKeysWithDictionary:card_groupDic];
//        [self.listArray addObject:model];
//        
//        NSDictionary *cardListDic = cardsArray[1];
//        NSArray *card_groupListArray = cardListDic[@"card_group"];
//        for (NSDictionary *dic in card_groupListArray) {
//            NearListModel *model = [[NearListModel alloc]init];
//            NSDictionary *mblogDic = dic[@"mblog"];
//            [model setValuesForKeysWithDictionary:mblogDic];
//            [self.listArrayTwo addObject:model];
//        }
//        dispatch_async(dispatch_get_main_queue(), ^{
//            [self.tableView reloadData];
//        });
//    } error:nil];
  */
    [WBHttpRequest requestWithURL:kNearMainUrl httpMethod:@"GET" params:nil delegate:self withTag:@"1"];
    
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 10;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[FindMainTableViewCell class] forCellReuseIdentifier:@"FindMainViewCell"];
    [_tableView registerClass:[NearListTableViewCell class] forCellReuseIdentifier:@"NearListCell"];
//    self.view.backgroundColor = [UIColor redColor];
    // Do any additional setup after loading the view.
}
- (void)moreAction:(UIBarButtonItem *)barButton{
    NSLog(@"more");
}

- (void)backAction:(UIBarButtonItem *)barButton{
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 44;
    }else if (indexPath.row != 4){
        NearListModel *model = _listArrayTwo[indexPath.row];
        return model.height;
    }else{
        return 0;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.listArray.count;
    }else{
        return self.listArrayTwo.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        FindMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FindMainViewCell" forIndexPath:indexPath];
        cell.model = _listArray[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
    }else{
        NearListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NearListCell" forIndexPath:indexPath];
        cell.model = _listArrayTwo[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
    }
}

- (void)request:(WBHttpRequest *)request didFinishLoadingWithDataResult:(NSData *)data {

    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSArray *cardsArray = dic[@"cards"];
    NSDictionary *cardsDic = cardsArray[0];
    NSArray *card_group = cardsDic[@"card_group"];
    NSDictionary *card_groupDic = card_group[0];
    FindMainTableViewModel *model = [[FindMainTableViewModel alloc]init];
    [model setValuesForKeysWithDictionary:card_groupDic];
    [self.listArray addObject:model];

    NSDictionary *cardListDic = cardsArray[1];
    NSArray *card_groupListArray = cardListDic[@"card_group"];
    for (NSDictionary *dic in card_groupListArray) {
        NearListModel *model = [[NearListModel alloc]init];
        NSDictionary *mblogDic = dic[@"mblog"];
        [model setValuesForKeysWithDictionary:mblogDic];
        [self.listArrayTwo addObject:model];
    }
    [self.tableView reloadData];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
